authApp.value('page', 'extraction_problem'); 
authApp.controller("chooseSub",function($scope,$http,$location,$getQueryString,Upload, $timeout,$sce,$cookieStore,$cookies,$interval){
	$scope.isKemu=true;
	$scope.isxb=false;
	//设置的变量
	var randomSub="";
	$scope.endChooseNum=$sce.trustAsHtml("点击 开始 选择试题");
	$scope.endNum=0;
	$scope.beginToChoose=true;
	$scope.isAleadyEnd=true;
	$scope.fTitle="";
	$scope.isLevelOne=true;
	$scope.totalXXNum=0;
	$scope.majorId="";
	$scope.majorName="";
	$scope.taskId="";
	$scope.taskName="";
	$scope.isSubjectPage=true;
	$scope.acticleList=[];
	$scope.showSubject = false;
	$scope.isXueqian = true;
	$scope.subjects = [];

	//查询该校专业
	$scope.getMajor=function(){
		$http({
			method:'get',
			url:apis.querymajor,
			data:{},
		}).success(function(response){
			if(response.code=="000000"){
				if(response.data=="0"){
					$scope.isXueqian=true;
				}else
					$scope.isXueqian = false;
				$scope.majorId = response.data;//获取专业Id
				$scope.getAllSpecialTasks();
				$scope.getAllSpecialTasks1();
			}else{
				alert(response.errorMsg);
			}
		})
	}
	
	//查询所有必测的专业
	$scope.getAllSpecialTasks=function(){
		$http({
			method:'get',
			url:apis.queryAuthSpecialTaskByType,
			params:{'major':$scope.majorId},
		}).success(function(response){
			if(response.code="000000"){
				$scope.ffid=response.data.ffid;
				//专业列表data数据
				$scope.firstList=response.data;
				
			}else{
				alert(response.errorMsg);
			}
		})
	}
	

	//查询所有选测的专业
	$scope.getAllSpecialTasks1=function(){
		$http({
			method:'get',
			url:apis.queryAuthSpecialTaskByType1,
			params:{'major':$scope.majorId},
		}).success(function(response){
			if(response.code="000000"){
				$scope.ffid=response.data.ffid;
				//专业列表data数据
				$scope.secondList=response.data;
				
			}else{
				alert(response.errorMsg);
			}
		})
	}
	
	
	$scope.myFunc = function(){
		if($scope.zyjn=="0"){
			$scope.isxb=true;
		}else{
			$scope.isxb=false;
		}
	}
	
	//设置taskId和taskName
	$scope.setMarjorInfo=function(list){
        $scope.taskId=list.pid;
        $scope.taskName=list.name;
		if(list.remark && list.remark  =="0"){
            $scope.beginToChoose=false;
            $scope.queryExamByTaskId("");
		}else{
			if($scope.isXueqian){
				$scope.beginToChoose=false;
	            $scope.queryExamByTaskId("");
			}else{
				//不是学前,需要先选择学科
				$scope.subjects = [];
				$scope.querySubjects();
				$scope.showSubject = true;
			}
		}
	}
	
	//根据taskID查询所有的专业
	$scope.querySubjects=function(){
		$http({
			method:'get',
			url:apis.querySubject,
			params:{'taskId':$scope.taskId},
		}).success(function(response){
			if(response.code="000000"){
				if(response.data){
					var list=response.data;
					
					for(var i=0;i<list.length;i++) {
						var val = "";
						if($scope.taskId == "20037"){//中学选修实验
							switch(list[i]){
							case "14":
								val="初中化学";
								break;
							case "15":
								val="初中生物";
								break;
							case "16":
								val="初中物理";
								break;
							case "17":
								val="高中化学";
								break;
							case "18":
								val="高中生物";
								break;
							case "19":
								val="高中物理";
							}
						}else if($scope.taskId == "20033"){//中学必修说课
							switch(list[i]){
							case "1":
								val="物理";
								break;
							case "2":
								val="美术";
								break;
							case "3":
								val="数学";
								break;
							case "4":
								val="体育";
								break;
							case "5":
								val="音乐";
								break;
							case "6":
								val="英语";
								break;
							case "7":
								val="语文";
								break;
							case "8":
								val="地理";
								break;
							case "9":
								val="化学";
								break;
							case "10":
								val="历史";
								break;
							case "11":
								val="信息技术";
								break;
							case "12":
								val="生物";
								break;
							case "13":
								val="政治";
							}
						}else if($scope.taskId == "20038"){
							switch(list[i]){
							case "5":
								val="音乐";
								break;
							case "4":
								val="体育";
								break;
							case "2":
								val="美术";
								break;
							}
						}
						
						var subject={key:list[i],value:val};
						$scope.subjects.push(subject); 
			        }  
				}
					
					//页面显示没有题库
				
			}else{
				alert(response.errorMsg);
			}
		})
	}
		
	//选择科目：点击取消
	$scope.hideSub=function(){
		$scope.showSubject=false;
		$scope.isLevelOne=true;
	}
	
	//根据专业技能查询所有的试题
	$scope.queryExamByTaskId=function(subject){
        $scope.showSubject = false;
        $scope.beginToChoose=false;
		if(subject=="1"){
			$scope.subjectName="物理";
		}else if(subject=="2"){
			$scope.subjectName="美术";
		}else if(subject=="3"){
			$scope.subjectName="数学";
		}else if(subject=="4"){
			$scope.subjectName="体育";
		}else if(subject=="5"){
			$scope.subjectName="音乐";
		}else if(subject=="6"){
			$scope.subjectName="英语";
		}else if(subject=="7"){
			$scope.subjectName="语文";
		}else if(subject=="8"){
			$scope.subjectName="地理";
		}else if(subject=="9"){
			$scope.subjectName="化学";
		}else if(subject=="10"){
			$scope.subjectName="历史";
		}else if(subject=="11"){
			$scope.subjectName="信息技术";
		}else if(subject=="12"){
			$scope.subjectName="生物";
		}else if(subject=="13"){
			$scope.subjectName="政治";
		}else if(subject=="14"){
			$scope.subjectName="初中化学";
		}else if(subject=="15"){
			$scope.subjectName="初中生物";
		}else if(subject=="16"){
			$scope.subjectName="初中物理";
		}else if(subject=="17"){
			$scope.subjectName="高中化学";
		}else if(subject=="18"){
			$scope.subjectName="高中生物";
		}else if(subject=="19"){
			$scope.subjectName="高中物理";
		}
		
		$http({
			method:'get',
			url:apis.queryExamByTaskId,
			params:{'taskId':$scope.taskId,'subject':subject},
		}).success(function(response){
			if(response.code=="000000"){
				//重置选择试题
				$scope.endChooseNum=$sce.trustAsHtml("点击 开始 选择试题");
				if(randomSub!=""){
					$interval.cancel(randomSub);
					randomSub="";
				}
				
				$scope.numDate=response.data.length;
				$scope.isSubjectPage=false;
				$scope.isLevelOne=false;
				$scope.fTitle=$scope.taskName;
				$scope.insidetList=response.data;
				$scope.totalXXNum=response.data.length;
				$scope.acticleList=response.data;

                $scope.isAleadyEnd=true;
                if($scope.totalXXNum!=0){
                    if(randomSub==""){
                        randomSub=$interval(function(){
                            var x=Math.ceil(Math.random()*$scope.totalXXNum);
                            $scope.endNum=x;
                            //$scope.endChooseNum=$scope.chooseSubjectList[x-1].fileName;
                            $scope.endChooseNum=$sce.trustAsHtml("选中第 <span style='font-size:40px;padding:0 10px;'>"+$scope.endNum+"</span> 套题");
                        }, 100)
                    }
                }else{
                    $scope.endChooseNum=$sce.trustAsHtml("<span>没有可选择的试题</span>");
                }
			}else{
				alert(response.errorMsg);
			}
		})
	}
	
	//点击开始选择试题
	$scope.chooseSubject=function(){
		$scope.isAleadyEnd=true;
		if($scope.totalXXNum!=0){
			if(randomSub==""){
				randomSub=$interval(function(){
					var x=Math.ceil(Math.random()*$scope.totalXXNum);
					$scope.endNum=x;
					//$scope.endChooseNum=$scope.chooseSubjectList[x-1].fileName;
					$scope.endChooseNum=$sce.trustAsHtml("选中第 <span style='font-size:40px;padding:0 10px;'>"+$scope.endNum+"</span> 套题");
				}, 100)
			}
		}else{
			$scope.endChooseNum=$sce.trustAsHtml("<span>没有可选择的试题</span>");
		}
	}
	//点击结束选择试题
	$scope.cancleChooseSubject=function(num){
		if(randomSub!=""){
			$interval.cancel(randomSub);
			randomSub="";
			$scope.isAleadyEnd=false;
			$scope.isLevelOne=true;
			$scope.subject="";
			//$scope.endChooseNum=$scope.chooseSubjectList[$scope.endNum-1].fileName;
			//alert($scope.chooseSubjectList[$scope.endChooseNum-1].fileName);
		}
	}
	//点击取消
	$scope.hideChoose=function(){
		$scope.beginToChoose=true;
		$scope.isAleadyEnd=true;
		if($scope.majorId=="2"&&($scope.taskId=="20033"||$scope.taskId=="20028"))//中学说课 中学实验
			$scope.showSubject=true;
		else
			$scope.isLevelOne=true;
		$scope.subject="";
		//重置选择试题
		$scope.endChooseNum=$sce.trustAsHtml("点击 开始 选择试题");
		if(randomSub!=""){
			$interval.cancel(randomSub);
			randomSub="";
		}
	}
	//返回专业页面
	$scope.goback=function(){
		$scope.isLevelOne=true;
		$scope.isSubjectPage=true;
	}
	
	
	//保存抽到的试题
	$scope.sureAndDownload=function(){
		var examId=$scope.acticleList[parseInt($scope.endNum)-1].pid;
		$http({
			method:'get',
			url:apis.saveExams,
			params:{'examId':$scope.acticleList[parseInt($scope.endNum)-1].pid},
		}).success(function(response){
			if(response.code="000000"){
				$scope.beginToChoose=true;
				$scope.isAleadyEnd=true;
				$scope.endChooseNum=$sce.trustAsHtml("点击 开始 选择试题");
				$scope.endNum=0;
				if(randomSub!=""){
					$interval.cancel(randomSub);
					randomSub="";
				}
				alert("试题保存成功！");	
				location.href=apis.downloadExam+"?examId="+examId;			
				$scope.goback();
			}else{
				alert(response.errorMsg);
			}
		})
	}
	$scope.getMajor();
})